<?php

class Event extends CI_Controller {

	function __construct()
	{
		parent:: __construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Event_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;

	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/event/index';
		$config['total_rows'] = count($this->Event_model->getEvent('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
			echo $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Event';
		$data['template'] = 'event/index';
		$data['res'] = $this->Event_model->getEvent('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}
	
	function add()
	{
		$this->form_validation->set_rules('nm_event', 'Nama Event','required');
		$this->form_validation->set_rules('desk', 'Deskripsi Event','required');
		$this->form_validation->set_rules('date_event', 'Tanggal Event','required');
		$this->form_validation->set_rules('time_event', 'Waktu Event', 'required');
		$this->form_validation->set_rules('location', 'Lokasi','required');
		$this->form_validation->set_rules('quota', 'Kapasitas','required|numeric');
		$this->form_validation->set_rules('date_open', 'Tanggal Buka','required');
		$this->form_validation->set_rules('date_closed', 'Tanggal Tutup','required');
		// untuk kelas
		$this->form_validation->set_rules('class_name[]', 'Kelas','required');
		$this->form_validation->set_rules('cost[]', 'Cost','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Event';
			$data['template'] = 'event/add';
			$this->load->view('backend/index',$data);
		}
		else
		{			
			$data = array
				(
				'nm_event'=>$this->input->post('nm_event'),
				'desc'=>$this->input->post('desk'),
				'date_event'=>$this->input->post('date_event'),
				'time_event'=>$this->input->post('time_event'),
				'location'=>$this->input->post('location'),
				'quota'=>$this->input->post('quota'),
				'date_open'=>$this->input->post('date_open'),
				'date_closed'=>$this->input->post('date_closed'),
				'image_url'=>'image_url',
				'image_url'=>'image_url_thumb',
				'image_slide'=>'image_slide'
				);
			$this->Event_model->addEvent($data);
			
			$className = $this->input->post('class_name');
			$cost = $this->input->post('cost');
			$percentDisc = $this->input->post('percent_disc');
			$idEvent = mysql_insert_id();
			
			foreach($className as $a => $b){
				if ($percentDisc[$a] == ''){
					$percentDisc[$a] = 0;
				}			
				$dataKelas = array(
					'id_event' => $idEvent,
					'class_name' => $className[$a],
					'cost' => $cost[$a],
					'percent_disc' => $percentDisc[$a],
					'percent_ammount' => $percentDisc[$a]
				);
				$this->Event_model->addEventClass($dataKelas);
			}
			
			$data['msg_url'] = '';
			$data['msg_slide'] = '';			
			$data['id_event'] = $idEvent;
			$data['nm_event'] = $this->input->post('nm_event');
			$data['title'] = 'Event';
			$data['template'] = 'event/upload';
			$this->load->view('backend/index',$data);
		}	
	}

	function upload(){
		if($this->input->post('upload')){		
			$id_event = $this->input->post('id_event');
			$image_url = 'image_url';
			$image_slide = 'image_slide';		
			// untuk URL
			$config = array(
				'upload_path' => './assets/images/event/',
				'allowed_types' => 'jpg|png|jpeg',
				'max_size'	=> 2000,
				'overwrite' => true,
				'file_name' => $id_event.'.jpg'				
			);			
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload($image_url)){
				$data['msg_url'] = $this->upload->display_errors();				
			}else{						
				$update = array('image_url' => $id_event.'.jpg');
				$this->Event_model->editEvent($id_event,$update);	
				$data['msg_url'] = 'Upload Image Url Sukses';
			}					
			// untuk thumbnail
			$image_data = $this->upload->data($image_url);
			$config = array(
				'source_image' => $image_data['full_path'],
				'new_image' => './assets/images/event/',
				'maintain_ration' => false,
				'create_thumb' => true,
				'width' => 100,
				'height' => 80
			);			
			$this->load->library('image_lib', $config);
			if($this->image_lib->resize()){
				$update = array('image_url_thumb' => $id_event.'_thumb.jpg'); 
				$this->Event_model->editEvent($id_event,$update);
			}
			
			
			/*
			//untuk Slide
			$config = array(
				'upload_path' => './assets/images/',
				'allowed_types' => 'jpg|png',
				'max_size'	=> 2000,
				'file_name' => 'slide'.$id_event.'.jpg'				
			);
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload($image_slide)){
				$data['msg_url'] = $this->upload->display_errors();				
			}else{
				$data['msg_slide'] = 'Upload Image Slide Sukses';
			}			
			*/
			
			if (($_FILES["image_slide"]["type"] == "image/jpeg")
			|| ($_FILES["image_slide"]["type"] == "image/jpg")
			|| ($_FILES["image_slide"]["type"] == "image/png")) {			
				$size = getimagesize($_FILES["image_slide"]["tmp_name"]);
				if ($size[0] == 950 && $size[1] == 370){
					copy($_FILES["image_slide"]["tmp_name"],"assets/images/event/" .$id_event.'_slide.jpg');
					$update = array('image_slide'=>$id_event.'_slide.jpg');
					$this->Event_model->editEvent($id_event,$update);
					$data['msg_slide'] = 'Upload Image Slide Sukses';
				}else{
					$data['msg_slide'] = 'Upload Image Slide Gagal! Dimensi tidak sesuai.';
				}				
			}else{
				$size = getimagesize($_FILES["image_slide"]["tmp_name"]);
				if($_FILES["image_slide"]["size"] > 20000)		$data['msg_slide'] = 'Upload Image Slide Gagal! Ukuran file terlalu besar.';
				else $data['msg_slide'] = 'Upload Image Slide Gagal! Harus gambar berformat jpeg, jpg atau png.';
			}
			$dir = './assets/images/event/'	;
			if(file_exists($dir.$id_event.'.jpg') && file_exists($dir.$id_event.'_slide.jpg')){
				$this->session->set_flashdata('message_type','<div id="kotak" class="success">Event berhasil ditambahkan.</div>');
				redirect('backend/event');
			}
			
			$data['id_event'] = $id_event;
			$data['nm_event'] = $this->input->post('nm_event');
			$data['title'] = 'Event';
			$data['template'] = 'event/upload';
			$this->load->view('backend/index',$data);		
		}
		else{
			$data['msg_url'] = '';	
			$data['msg_slide'] = '';
			if($this->input->post('id_event')){
				$data['id_event'] = $this->input->post('id_event');
				$data['nm_event'] = $this->input->post('nm_event');
			} else{
				$id = $this->uri->segment(4);
				$data['id_event'] = $id;
				
				$row = $this->Event_model->getEvent(FALSE,$id,FALSE,FALSE);
				$data['nm_event'] = $row->nm_event;
			}
			$data['title'] = 'Event';
			$data['template'] = 'event/upload';
			$this->load->view('backend/index',$data);
		}
	}
	
	function edit()	
	{
		$this->form_validation->set_rules('nm_event', 'Nama Event','required');
		$this->form_validation->set_rules('desk', 'Deskripsi Event','required');
		$this->form_validation->set_rules('date_event', 'Tanggal Event','required');
		$this->form_validation->set_rules('time_event', 'Waktu Event', 'required');
		$this->form_validation->set_rules('location', 'Lokasi','required');
		$this->form_validation->set_rules('quota', 'Kapasitas','required|numeric');
		$this->form_validation->set_rules('date_open', 'Tanggal Buka','required');
		$this->form_validation->set_rules('date_closed', 'Tanggal Tutup','required');		
		/*		
		$this->form_validation->set_rules('class_name[]', 'Kelas','required');
		$this->form_validation->set_rules('cost[]', 'Cost','required');
		*/
		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Event';
			$data['template'] = 'event/edit';	
			$data['kelas'] = $this->Event_model->getClass($id);
			$data['row'] = $this->Event_model->getEvent(FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else{	
			$id = $this->uri->segment(4);
			$data = array(
				'nm_event'=>$this->input->post('nm_event'),
				'desc'=>$this->input->post('desk'),
				'date_event'=>$this->input->post('date_event'),
				'time_event'=>$this->input->post('time_event'),
				'location'=>$this->input->post('location'),
				'quota'=>$this->input->post('quota'),
				'date_open'=>$this->input->post('date_open'),
				'date_closed'=>$this->input->post('date_closed')
				);
			
		
			$this->Event_model->editEvent($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Event '.$this->input->post('nm_event').' berhasil diupdate</div>');
			redirect('backend/event');
		}
			
	}

	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Event_model->deleteEvent($id);
		$count = count($this->Event_model->getEvent('all',FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('event','delete',$uri,$count);
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["event_id"] as $id):
			$this->db->where('id_event',$id);
			$this->db->delete('tk_event');
		endforeach;
	}
}